package com.carrental.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.rental.dto.ContractDTO;
import com.carrental.rental.entity.Contract;
import com.carrental.rental.service.ContractService;
import com.carrental.rental.vo.ContractVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 合同控制器
 */
@Api(tags = "合同管理接口")
@RestController
@RequestMapping("/contracts")
public class ContractController {

    @Autowired
    private ContractService contractService;

    @ApiOperation("创建合同")
    @PostMapping
    public Long createContract(@Valid @RequestBody ContractDTO contractDTO) {
        return contractService.createContract(contractDTO);
    }

    @ApiOperation("根据ID获取合同详情")
    @ApiImplicitParam(name = "id", value = "合同ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ContractVO getContractById(@PathVariable Long id) {
        return contractService.getContractById(id);
    }

    @ApiOperation("根据合同号获取合同详情")
    @ApiImplicitParam(name = "contractNo", value = "合同号", required = true, dataType = "String", paramType = "path")
    @GetMapping("/no/{contractNo}")
    public ContractVO getContractByContractNo(@PathVariable String contractNo) {
        return contractService.getContractByContractNo(contractNo);
    }

    @ApiOperation("根据订单ID获取合同详情")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/order/{orderId}")
    public ContractVO getContractByOrderId(@PathVariable Long orderId) {
        return contractService.getContractByOrderId(orderId);
    }

    @ApiOperation("分页查询用户合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "合同状态", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/user")
    public Page<ContractVO> pageUserContracts(@RequestParam(defaultValue = "1") long current,
                                              @RequestParam(defaultValue = "10") long size,
                                              @RequestParam Long userId,
                                              @RequestParam(required = false) Integer status) {
        Page<Contract> page = new Page<>(current, size);
        return contractService.pageUserContracts(page, userId, status);
    }

    @ApiOperation("分页查询所有合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "contractNo", value = "合同号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "合同状态", dataType = "Integer", paramType = "query")
    })
    @GetMapping
    public Page<ContractVO> pageContracts(@RequestParam(defaultValue = "1") long current,
                                          @RequestParam(defaultValue = "10") long size,
                                          @RequestParam(required = false) String contractNo,
                                          @RequestParam(required = false) String orderNo,
                                          @RequestParam(required = false) Long userId,
                                          @RequestParam(required = false) Integer status) {
        Page<Contract> page = new Page<>(current, size);
        return contractService.pageContracts(page, contractNo, orderNo, userId, status);
    }

    @ApiOperation("签署合同")
    @ApiImplicitParam(name = "id", value = "合同ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/sign")
    public boolean signContract(@PathVariable Long id) {
        return contractService.signContract(id);
    }

    @ApiOperation("使合同失效")
    @ApiImplicitParam(name = "id", value = "合同ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/invalidate")
    public boolean invalidateContract(@PathVariable Long id) {
        return contractService.invalidateContract(id);
    }
} 